﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CourierManager;
using System.IO;

namespace TeamWPF
{
    /// <summary>
    /// Interaction logic for VehicleWindow.xaml
    /// </summary>
    public partial class VehicleWindow : Window
    {
        public VehicleWindow()
        {
            InitializeComponent();
        }

        private void ProcessVehicleType(object sender, RoutedEventArgs e)
        {
            if (sender as MenuItem == Truck)
            {
                DefaultType.Header = Truck.Header;
                DefaultType.Background = Brushes.Azure;
            }

            else
            {
                DefaultType.Header = Van.Header;
                DefaultType.Background = Brushes.Azure;
            }
        }
       
        private void ProcessVehicleData(object sender, RoutedEventArgs e)
        {
            string vehicleType = this.DefaultType.Header.ToString();

            string brand = null;
            if (this.NewVehicleBrandInput.Text != string.Empty)
            {
                brand = this.NewVehicleBrandInput.Text;               
            }
            else
            {
                ErrorWindow emptyBrandWindow = new ErrorWindow();
                emptyBrandWindow.ErrorText.Text = "Enter a valid brand!";
                emptyBrandWindow.ShowDialog();
                return;
            }

            TransportUnitState state = TransportUnitState.Available;

            string licensePlate = this.NewLicensePlateInput.Text.Trim().ToUpper();
            try
            {
                ValidatorLicensePlates.ValidateLicensePlates(licensePlate);
            }
            catch (ArgumentException)
            {
                ErrorWindow wrongLicensePlateWindow = new ErrorWindow();
                wrongLicensePlateWindow.ErrorText.Text = "Enter a valid license plate!";
                wrongLicensePlateWindow.ShowDialog();
                return;
            }

            double fuelConsumption = 0;
            try
            {
                fuelConsumption = double.Parse(this.NewFuelConsumptionInput.Text);
            }
            catch (FormatException)
            {
                ErrorWindow wrongFuelConsumpWindow = new ErrorWindow();
                wrongFuelConsumpWindow.ErrorText.Text = "Enter a valid fuel consumption!";
                wrongFuelConsumpWindow.ShowDialog();
                return;
            }

            string yearProduction = null;
            if (this.NewYearProductionInput.Text != string.Empty)
            {
                yearProduction = this.NewYearProductionInput.Text;
            }
            else
            {
                ErrorWindow emptyYearWindow = new ErrorWindow();
                emptyYearWindow.ErrorText.Text = "Enter a valid year of production!";
                emptyYearWindow.ShowDialog();
                return;
            }

            switch (vehicleType)
            {
                case "Truck":
                    Truck newTruckVehicle = new Truck(licensePlate, state);                  
                    newTruckVehicle.Type = vehicleType;
                    newTruckVehicle.Brand = brand;
                    newTruckVehicle.FuelConsumption = fuelConsumption;
                    newTruckVehicle.ProductionDate = yearProduction;
                    newTruckVehicle.WriteInDatabase();
                    newTruckVehicle.SaveVehicleData();
                    break;
                case "Van":
                    Van newVan = new Van(licensePlate, state);
                    newVan.Type = vehicleType;
                    newVan.Brand = brand;
                    newVan.FuelConsumption = fuelConsumption;
                    newVan.ProductionDate = yearProduction;
                    newVan.WriteInDatabase();
                    newVan.SaveVehicleData();
                    break;
                default:   
                    ErrorWindow emptyInputFieldsWindow = new ErrorWindow();
                    emptyInputFieldsWindow.ErrorText.Text = "Empty type field!";
                    emptyInputFieldsWindow.Show();
                    return;
            }
            Close();
        }
    }
}
